package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.model.vo.patrol.PatrolObjectStandardVo;
import com.hitqz.robot.api.common.entity.PatrolObjectStandard;
import com.hitqz.robot.biz.service.PatrolObjectStandardService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/7
 */

@RestController
@AllArgsConstructor
@RequestMapping("/patrolStandard")
@Tag(name = "巡检对象标准值管理")
public class PatrolObjectStandardController {

    private final PatrolObjectStandardService patrolObjectStandardService;


    @GetMapping("/{id}" )
    @Operation(summary = "根据id查询巡检对象标准值")
    public R<PatrolObjectStandardVo> getById(@PathVariable("id") Integer id) {
        PatrolObjectStandard patrolObjectStandard = patrolObjectStandardService.getById(id);
        PatrolObjectStandardVo vo = new PatrolObjectStandardVo();
        BeanUtil.copyProperties(patrolObjectStandard,vo,false);
        return R.ok(vo);
    }


    @PostMapping
    @Operation(summary = "新增巡检对象标准值")
    public R save(@RequestBody @Validated PatrolObjectStandard dto) {
        List<PatrolObjectStandard> list = patrolObjectStandardService.list(Wrappers.<PatrolObjectStandard>lambdaQuery().eq(PatrolObjectStandard::getPatrolObjectId, dto.getPatrolObjectId()));
        if (!list.isEmpty()){
            R.failed("不能重复添加标准值");
        }
        return R.ok(patrolObjectStandardService.save(dto));
    }

    @PutMapping
    @Operation(summary = "修改巡检对象标准值")
    public R updateById(@RequestBody @Validated PatrolObjectStandard dto) {
        return R.ok(patrolObjectStandardService.updateById(dto));
    }

    @DeleteMapping
    @Operation(summary = "根据id删除巡检对象标准值")
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(patrolObjectStandardService.removeBatchByIds(CollUtil.toList(ids)));
    }

}
